﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class ArticleBreadCrumb : Literal
    {
        protected Queue crumbs = new Queue();
        protected HtmlAnchor anchor;
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.NewsId);
        private int newsCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.NewsCategoryId);
        private int defaultIndex = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.DefaultIndex);

        protected override void CreateChildControls()
        {
            string url = SiteUrls.Instance().GetArticleListUrl(0);
            if (defaultIndex == 1)
            {
                url = SiteUrls.Instance().GetNewsPicListUrl();
            }
            crumbs.Enqueue(GetAnchor("资讯首页", url));
            if (entityId > 0)
            {
                News news = BusinessManager.GetNews(entityId);
                if (news != null)
                {
                    NewsCategory childNewsCategory = BusinessManager.GetNewsCategory(news.ChildNewsCategoryId.Value);
                    if (childNewsCategory != null)
                    {
                        NewsCategory parentNewsCategory = BusinessManager.GetNewsCategory(childNewsCategory.UpNewsCategoryId.Value);
                        if (parentNewsCategory != null)
                        {
                            crumbs.Enqueue(GetAnchor(parentNewsCategory.Title.Value, SiteUrls.Instance().GetArticleListUrl(parentNewsCategory.EntityId.Value)));
                        }
                        crumbs.Enqueue(GetAnchor(childNewsCategory.Title.Value, SiteUrls.Instance().GetArticleListUrl(childNewsCategory.EntityId.Value)));
                    }
                    crumbs.Enqueue(GetAnchor(news.Title.Value, SiteUrls.Instance().GetArticleUrl(entityId)));
                }
            }
            else if (newsCategoryId > 0)
            {
                NewsCategory newsCategory = BusinessManager.GetNewsCategory(newsCategoryId);
                if (newsCategory != null)
                {
                    NewsCategory parentNewsCategory = BusinessManager.GetNewsCategory(newsCategory.UpNewsCategoryId.Value);
                    if (parentNewsCategory != null)
                    {
                        crumbs.Enqueue(GetAnchor(parentNewsCategory.Title.Value, SiteUrls.Instance().GetArticleListUrl(parentNewsCategory.EntityId.Value)));
                    }
                    crumbs.Enqueue(GetAnchor(newsCategory.Title.Value, SiteUrls.Instance().GetArticleListUrl(newsCategory.EntityId.Value)));
                }
            }
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("您的位置:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));
                }
            }

        }

    }
}